package uoc.biblioteca.mappings.dao;

// Generated 11/04/2011 14:10:24 by Hibernate Tools 3.2.1.GA

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import uoc.biblioteca.mappings.object.FondoVo;
import uoc.biblioteca.mappings.object.UserVo;
import uoc.biblioteca.sessionFactory.HibernateSessionFactory;

/**
 * Home object for domain model class User.
 * @see uoc.biblioteca.mappings.dao.UserVo
 * @author Hibernate Tools
 */
public class UserHome {

	private static final Log log = LogFactory.getLog(UserHome.class);

	private final SessionFactory sessionFactory = null;

	protected SessionFactory getSessionFactory() {
		try {
			return (SessionFactory) new InitialContext()
					.lookup("SessionFactory");
		} catch (Exception e) {
			log.error("Could not locate SessionFactory in JNDI", e);
			throw new IllegalStateException(
					"Could not locate SessionFactory in JNDI");
		}
	}

	public void persist(UserVo transientInstance) {
		log.debug("persisting User instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(UserVo instance) {
		log.debug("attaching dirty User instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(UserVo instance) {
		log.debug("attaching clean User instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(UserVo persistentInstance) throws Exception {
		log.debug("deleting UserVo instance");
		try {
			
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();
			session.flush();
			tx = session.beginTransaction();
			session.delete(persistentInstance);
			tx.commit();
			
			log.debug("delete successful");
		} catch (Exception re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public UserVo merge(UserVo detachedInstance) {
		log.debug("merging UserVo instance");
		try {
			UserVo result = (UserVo) sessionFactory.getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public UserVo findById(java.lang.Integer id) {
		log.debug("getting UserVo instance with id: " + id);
		try {
			
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();
			session.flush();
			
			UserVo instance = (UserVo) session.get("uoc.biblioteca.mappings.object.UserVo", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(UserVo instance) {
		log.debug("finding UserVo instance by example");
		try {
			List results = sessionFactory.getCurrentSession().createCriteria("uoc.biblioteca.mappings.object.UserVo").add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public UserVo[] getAllUsers() {
		
		/* will hold the books we are going to return later */
		try{
			List events = new ArrayList();
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			List tmpEvents = session.createQuery("select c from uoc.biblioteca.mappings.object.UserVo as c order by c.name").list();
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				UserVo userTemp = (UserVo) iter.next();
				userTemp.getRol();
				events.add(userTemp);
			}
			tx.commit();
			
			return (UserVo[]) events.toArray(new UserVo[0]);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public UserVo testPwdByUser(String name,String pwd) {
		
		/* will hold the books we are going to return later */
		try{
			UserVo user = null;
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();
			session.clear();
			
			//String hql = "select c from User as c where c.name = '"+ name +"' order by c.name";
			String hql = "select c from uoc.biblioteca.mappings.object.UserVo as c where c.nick = '"+ name +"' order by c.name";

			tx = session.beginTransaction();
			List tmpEvents = session.createQuery(hql).list();
			
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				user = (UserVo) iter.next();
				if(!user.getPwd().equals(pwd)){
					user = null;
				}
				
			}
			tx.commit();
			return user;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void saveNew(UserVo instance) {
		
		log.debug("save new UserVo instance");
		
		try {
			
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();
			
			session.clear();
			
			tx = session.beginTransaction();
			
			session.saveOrUpdate(instance);
			
			tx.commit();
			session.close();
			
			log.debug("saveNew successful");
			
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	public UserVo findUserByDni(String dni) {
		
		/* will hold the books we are going to return later */
		try{
			UserVo user = null;
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();
			session.clear();
			
			String hql = "select c from uoc.biblioteca.mappings.object.UserVo as c where c.dni = '"+ dni +"' order by c.name";

			tx = session.beginTransaction();
			List tmpEvents = session.createQuery(hql).list();
			
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				user = (UserVo) iter.next();
			}
			tx.commit();
			return user;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	
	}
	
	public UserVo findUserByIdp(String idp) throws Exception{
		
		/* will hold the books we are going to return later */
		try{
			UserVo user = null;
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();
			
			session.clear();
			
			String hql = "select c from uoc.biblioteca.mappings.object.UserVo as c where c.idp = '"+ idp +"' order by c.name";

			tx = session.beginTransaction();
			List tmpEvents = session.createQuery(hql).list();
			
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				user = (UserVo) iter.next();
			}
			tx.commit();
			return user;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		
	
	}
	
	public UserVo findUserbyNick(String nick) {
		
		/* will hold the books we are going to return later */
		try{
			UserVo user = null;
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();
			session.clear();
			
			String hql = "select c from uoc.biblioteca.mappings.object.UserVo as c where c.nick = '"+ nick +"' order by c.name";

			tx = session.beginTransaction();
			List tmpEvents = session.createQuery(hql).list();
			
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				user = (UserVo) iter.next();
			}
			tx.commit();
			return user;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public UserVo findUserbySameNick(String nick,Integer id) {
		
		/* will hold the books we are going to return later */
		try{
			UserVo user = null;
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();
			session.clear();
			
			String hql = "select c from uoc.biblioteca.mappings.object.UserVo as c where c.nick = '"+ nick +"' and c.id != '"+ id +"'  order by c.name";

			tx = session.beginTransaction();
			List tmpEvents = session.createQuery(hql).list();
			
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				user = (UserVo) iter.next();
			}
			tx.commit();
			return user;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	public UserVo[] getAllUsersByFiltres(String idRol,String nickFiltre) {
		
		/* will hold the books we are going to return later */
		try{
			List events = new ArrayList();
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			
			String hql = "select c from uoc.biblioteca.mappings.object.UserVo as c where 1=1 ";
			
			if(!("0").equals(idRol)){
				hql += " and c.rol.idRol="+idRol;
			}
			
			if(!("").equals(nickFiltre)){
				hql += " and c.nick LIKE '%"+nickFiltre+"%'";
			}
			
			hql += " order by c.name";
			
			
			List tmpEvents = session.createQuery(hql).list();
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				UserVo userTemp = (UserVo) iter.next();
				userTemp.getRol();
				events.add(userTemp);
			}
			tx.commit();
			
			return (UserVo[]) events.toArray(new UserVo[0]);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean testConection() throws Exception{
		
		/* will hold the books we are going to return later */
		try{
			/* a Hibernate session */
			Session session = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();
			session.clear();

			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		
	
	}
	
}
